package com.whq.common;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 时间工具类
 * @author wanghuaiqiang  2008-11-12
 *
 */
public class DateUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
	
	/**
	 * 按格式把String转化成Date对象
	 * @param aMask 时间格式
	 * @param strDate
	 * @return
	 */
	public static final Date convertStringToDate(String aMask, String strDate){
		SimpleDateFormat df = null;
		Date date = null;
		if (strDate == null || "".equals(strDate)) {
			return date;
		}
		df = new SimpleDateFormat(aMask);
		try {
			date = df.parse(strDate);
		} catch (ParseException e) {
			LOGGER.error("parse String[" + strDate + "] to Date by Mask[" + aMask + "] excetion: ", e);
		}
		return date;
	}
	
	/**
	 * 按格式把Date转化成String对象
	 * @param aMask 时间格式
	 * @param aDate
	 * @return
	 */
	public static final String convertDateToString(String aMask, Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";
		if (aDate != null) {
			df = new SimpleDateFormat(aMask);
			returnValue = df.format(aDate);
		}
		return returnValue;
	}
	
	/**
	 * 按格式把Date转化成Integer对象
	 * @param aMask 时间格式
	 * @param aDate
	 * @return
	 */
	public static final Integer convertDateToInteger(String aMask, Date aDate) {
		String strDate = convertDateToString(aMask, aDate);
		Integer iDate = null;
		if (strDate != null && !"".equals(strDate)) {
			iDate = Integer.valueOf(strDate);
		}
		return iDate;
	}
	
	/**
	 * 按格式把Integer转化成Date对象
	 * @param aMask 时间格式
	 * @param aDate
	 * @return
	 */
	public static final Date convertIntegerToDate(String aMask, Integer iDate) {
		String strDate = String.valueOf(iDate);
		return convertStringToDate(aMask, strDate);
	}
	
	
	/**
     * 获得传入日期 date 偏移 offsetDay 天数的00:00:00
     * @param date
     * @param offsetDay 偏移天数
     * @return
     */
    public static Date getOffsetDateBegin(Date date, int offsetDay) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.add(Calendar.DATE, offsetDay);
        return c.getTime();
    }
    
    
    /**
	 * 得到date所在月份的第一天00:00:00
	 * @param date
	 * @return 
	 */
	public static Date getMonthFirstDateOfOneDate(Date date) {
	    Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.set(Calendar.DATE, 1);
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到date所在月份的最后一天00:00:00
	 * @param date
	 * @return
	 */
	public static Date getMonthLastDateOfOneDate(Date date) {
	    Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		c.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到 date 所在月份下月的第一天00:00:00
	 * @param date
	 * @return
	 */
	public static Date getNextMonthFirstDateOfOneDate(Date date) {
		Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.MONTH, 1);	// 加一个月
		c.set(Calendar.DATE, 1);	// 把日期设置为当月第一天
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到 date 所在月份上月的第一天00:00:00
	 * @param date
	 * @return
	 */
	public static Date getBeforeMonthFirstDateOfOneDate(Date date) {
		Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.MONTH, -1);	// 加一个月
		c.set(Calendar.DATE, 1);	// 把日期设置为当月第一天
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到 date 所在周的周一00:00:00
	 * @param date
	 * @return
	 */
	public static Date getWeekMondayOfOneDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		// 因为按中国礼拜一作为第一天所以这里减1
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	
	/**
	 * 获取日期之间每月00:00:00的日期List
	 * @param dt1
	 * @param dt2
	 * @return
	 */
	public static List<Date> getMonthFirstDateListBetween2Day(Date dt1, Date dt2) {
		List<Date> retList = new ArrayList<Date>();
		if ( (dt1 == null) 
				|| (dt2 == null) 
				) {
			return retList;
		}
		Date beginDt = new Date();
		Date endDt = new Date();
		if (dt1.getTime() > dt2.getTime()) {
			beginDt.setTime(dt2.getTime());
			endDt.setTime(dt1.getTime());
		} else {
			beginDt.setTime(dt1.getTime());
			endDt.setTime(dt2.getTime());
		}
		
		Date oneDate = getMonthFirstDateOfOneDate(beginDt);
		retList.add(oneDate);
		
		oneDate = getNextMonthFirstDateOfOneDate(beginDt);
		while (oneDate.getTime() <= endDt.getTime()) {
			 retList.add(oneDate);
			 
			 oneDate = getNextMonthFirstDateOfOneDate(oneDate);
		}
		
		return retList;
	}
	
	/**
     * 获取日期之间每天00:00:00的日期List
     * @param dt1
     * @param dt2
     * @return
     */
    public static List<Date> getDateListBetween2Day(Date dt1, Date dt2) {
        List<Date> retList = new ArrayList<Date>();
        if ( (dt1 == null) 
                || (dt2 == null) 
                ) {
            return retList;
        }
        Date beginDt = new Date();
        Date endDt = new Date();
        if (dt1.getTime() > dt2.getTime()) {
            beginDt.setTime(dt2.getTime());
            endDt.setTime(dt1.getTime());
        } else {
            beginDt.setTime(dt1.getTime());
            endDt.setTime(dt2.getTime());
        }
        
        Date oneDate = getOffsetDateBegin(beginDt, 0);
        while (oneDate.getTime() <= endDt.getTime()) {
             retList.add(oneDate);
             oneDate = getOffsetDateBegin(oneDate, 1);
        }
        
        return retList;
    }
	
	/**
	 * 获取某个日期 dt 对应的星期(0-星期天, 1-星期一......)
	 * @param dt
	 * @return
	 */
	public static int getWeekOfDate(Date dt) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dt);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek < 0) dayOfWeek = 0;
		return dayOfWeek;
	}
    
}
